package com.whmnx.chitchat.ui.chat;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;

import com.whmnx.chitchat.R;
import com.whmnx.chitchat.base.BaseActivity;
import com.whmnx.chitchat.bean.LoginBean;
import com.whmnx.chitchat.bean.UserBean;
import com.whmnx.chitchat.constants.Constants;
import com.whmnx.chitchat.http.CallBack;
import com.whmnx.chitchat.http.HttpClient;
import com.whmnx.chitchat.utils.SPUtil;
import com.whmnx.chitchat.utils.UItils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * author : huang
 * date   : 2020/7/21 0021 19:21
 * version: 1.0
 *
 * @author Administrator
 */
public class AddMoreActivity extends BaseActivity {
    @BindView(R.id.message_search)
    EditText query;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.contact_add_activity);
        setHead_title(View.GONE);
        tv_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


            }
        });
    }

    @Override
    protected void loadData() {
        query.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                //点击搜索的时候隐藏软键盘
                // hideKeyboard(EditText);
                // 在这里写搜索的操作,一般都是网络请求数据
                if (!TextUtils.isEmpty(v.getText().toString().trim())) {
                    search(v.getText().toString().trim());
                }
                return true;
            }
            return false;
        });
    }

    private void search(String trim) {
        if (!UItils.isPhoneNumber(trim)) {
            UItils.showToastSafe("请输入正确的手机号码");
            return;
        }
        if (trim.equals(SPUtil.getString(Constants.PHONE))){
            UItils.showToastSafe("自己不能添加自己");
            return;
        }

        Map<String, Object> params = new HashMap<>();
        params.put("Con", trim);
        HttpClient.get(AddMoreActivity.this, Constants.GETUSERLIST, params, new CallBack<List<UserBean>>() {
            @Override
            public void onSuccess(List<UserBean> result) {
                if (result.size() == 0){
                    UItils.showToastSafe("暂无相关信息");
                    return;
                }
                Map<String, Object> params = new HashMap<>();
                params.put("ToUserID", result.get(0).getUserId());
                params.put("UserID", SPUtil.getString(Constants.USERID));
                HttpClient.get(AddMoreActivity.this, Constants.GETISFRIEND, params, new CallBack<List<UserBean>>() {
                    @Override
                    public void onSuccess(List<UserBean> loginBean) {
                            Intent intent = new Intent();
                            intent.putExtra("result", result.get(0));
                            if (loginBean.size() > 0) {
                                intent.putExtra("state", loginBean.get(0).getStatus());
                            }
                            intent.setClass(AddMoreActivity.this, FriendInfoActivity.class);
                            startActivity(intent);

                       /* if (loginBean.size() == 0){  //不是好友
                            Intent intent = new Intent();
                            intent.putExtra("result", result.get(0));
                            intent.setClass(AddMoreActivity.this, FriendInfoActivity.class);
                            startActivity(intent);
                        }else {
                            if (loginBean.get(0).getStatus() == 2) {
                                UItils.showToastSafe("已经是好友");
                            }else if (loginBean.get(0).getStatus() == 3){
                                UItils.showToastSafe("已拉黑");
                            }
                        }*/
                    }
                });
            }
        });
    }



    @OnClick(R.id.tv_cancel)
    public void onViewClicked() {
        finish();
    }
}
